home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / tag_env / tagenv.bas < prev    next >
BASIC Source File  |  1995-09-06  |  7KB  |  246 lines

  1.  
  2. '   TAGENV.BAS
  3.  
  4. '   REQUIRES:
  5. '             STRTOK.BAS
  6.  
  7.  
  8. '   TagString subsystem:
  9. '
  10. '   This set of routines provides support for tagged string fields
  11. '   in a VB Form or Control Tag property.
  12. '
  13. '   The Tag property, under this support, consists of a string
  14. '   of keyword=value pairs, delimited by semicolons;  for instance,
  15. '   the following might be a tag string:
  16. '
  17. '   formname=myForm;myname="Thomas A. Dacon";graphsize=large
  18. '
  19. '   You delete a string from a tagged string field by setting it
  20. '   to a null string, just like the SET command in DOS.
  21. '
  22. '   Keywords and contents fields are stored in mixed case, as supplied,
  23. '   but searches for keywords are case-insensitive.
  24.  
  25. '   The API:
  26. '
  27. '   SetFormTagString <form>,    key$, contents$
  28. '   GetFormTagString <form>,    key$, contents$
  29. '
  30. '   SetCtlTagString  <control>, key$, contents$
  31. '   GetCtlTagString  <control>, key$, contents$
  32. '
  33.  
  34. Function ExtractKey$ (theSubString As String)
  35. '
  36. '   Returns the keyword portion of a
  37. '   keyword=value string "kkk=vvvvv"
  38. '
  39.     Dim i As Integer
  40.     Dim theKey As String
  41.  
  42.     i = InStr(theSubString, "=")
  43.     If i <> 0 Then
  44.         theKey = Left$(theSubString, i - 1)
  45.     Else
  46.         theKey = ""
  47.     End If
  48.  
  49.     ExtractKey$ = theKey
  50.  
  51. End Function
  52.  
  53. Function ExtractKeyValue$ (theSubString As String)
  54. '
  55. '   Returns the value portion of a
  56. '   keyword=value string "kkk=vvvvv"
  57. '
  58.  
  59.     Dim i As Integer
  60.     Dim theContents As String
  61.  
  62.     i = InStr(theSubString, "=")
  63.     If i <> 0 Then
  64.         theContents = Mid$(theSubString, i + 1)
  65.     Else
  66.         theContents = ""
  67.     End If
  68.  
  69.     ExtractKeyValue$ = theContents
  70.  
  71. End Function
  72.  
  73. Sub GetCtlTagString (c As Control, key As String, contents As String)
  74. '
  75. '   Get the current value of a key=contents field
  76. '   in a Control's Tag property.  A null string is
  77. '   returned if the key is not found.
  78. '
  79.     GetTagSubstring (c.Tag), key, contents
  80.  
  81. End Sub
  82.  
  83. Sub GetFormTagString (f As Form, key As String, contents As String)
  84. '
  85. '   Get the current value of a key=contents field
  86. '   in a Form's Tag property.  A null string is
  87. '   returned if the key is not found.
  88. '
  89.     GetTagSubstring (f.Tag), key, contents
  90.  
  91. End Sub
  92.  
  93. Sub GetTagSubstring (theTagString As String, key As String, contents As String)
  94. '
  95. '   Internal routine to retrieve the contents of a key=contents
  96. '   field in a string variable.
  97. '
  98.     Dim thisString As String
  99.     Dim subString As String
  100.  
  101.     contents = ""   'in case we don't find the key
  102.  
  103.     If theTagString <> "" Then
  104.         thisString = theTagString
  105.         Do
  106.             subString = StrTok$(thisString, ";")
  107.             thisString = ""
  108.             If subString <> "" Then
  109.                 If UCase$(ExtractKey$(subString)) = UCase$(key) Then
  110.                     contents = ExtractKeyValue$(subString)
  111.                     Exit Do
  112.                 End If
  113.             End If
  114.         Loop Until subString = ""
  115.     End If
  116.  
  117. End Sub
  118.  
  119. Function ParseKeywordValue (text As String, keyword As String, keyvalue As String) As Integer
  120. '
  121. '   Given a text string of the form:
  122. '           keyword = value
  123. '       or  keyword = "value"
  124. '   parses the keyword and value into the output arguments,
  125. '   stripping leading and trailing blanks, and removing the
  126. '   optional double quotes from the value field.
  127. '
  128. '   Returns Boolean("=" character present, following a non-blank field)
  129. '
  130.     Dim eqPos As Integer
  131.     Dim quotes As String * 1
  132.  
  133.     eqPos = InStr(text, "=")
  134.     If eqPos > 0 Then
  135.         keyword = LTrim$(RTrim$(Left$(text, eqPos - 1)))
  136.         keyvalue = LTrim$(RTrim$(Mid$(text, eqPos + 1)))
  137.         quotes = Chr$(34)
  138.         If Left$(keyvalue, 1) = quotes And Right$(keyvalue, 1) = quotes Then
  139.             keyvalue = Mid$(keyvalue, 2, Len(keyvalue) - 2)
  140.         End If
  141.     End If
  142.  
  143.     ParseKeywordValue = (eqPos > 0) And (keyword <> "")
  144.  
  145. End Function
  146.  
  147. Sub SetCtlTagString (c As Control, key As String, contents As String)
  148. '
  149. '   Insert, replace, or delete a key=contents field
  150. '   in a Control's Tag property.
  151. '
  152.     Dim theTagString As String
  153.  
  154.     theTagString = c.Tag
  155.     SetTagSubstring theTagString, key, contents
  156.     c.Tag = theTagString
  157.  
  158. End Sub
  159.  
  160. Sub SetFormTagString (f As Form, key As String, contents As String)
  161. '
  162. '   Insert, replace, or delete a key=contents field
  163. '   in a Form's Tag property.
  164. '
  165.     Dim theTagString As String
  166.  
  167.     theTagString = f.Tag
  168.     SetTagSubstring theTagString, key, contents
  169.     f.Tag = theTagString
  170.  
  171. End Sub
  172.  
  173. Sub SetTagSubstring (theTagString As String, key As String, contents As String)
  174. '
  175. '   Internal routine to insert, replace, or delete
  176. '   a key=contents field in a string variable.
  177. '
  178.     Dim tagStringAccumulator As String
  179.     Dim thisString As String
  180.     Dim subString As String
  181.     Dim theKey As String
  182.     Dim substringToAdd As String
  183.  
  184.     tagStringAccumulator = ""
  185.  
  186.     If theTagString <> "" Then
  187.         thisString = theTagString
  188.         foundIt = False
  189.         Do
  190.             subString = StrTok$(thisString, ";")
  191.             thisString = ""              'for subsequent strtok calls
  192.             If subString <> "" Then
  193.                 If Not foundIt Then
  194.                     theKey = ExtractKey$(subString)
  195.                     If theKey <> key Then
  196.                         substringToAdd = subString
  197.                         GoSub AddSubstring
  198.                     Else    'this deletes if new contents = ""
  199.                         foundIt = True
  200.                         If contents <> "" Then
  201.                             substringToAdd = key + "=" + contents
  202.                             GoSub AddSubstring
  203.                         End If
  204.                     End If
  205.                 Else
  206.                     substringToAdd = subString
  207.                     GoSub AddSubstring
  208.                 End If
  209.             End If
  210.         Loop Until subString = ""
  211.  
  212.         '   If we didn't find the key, we need to add the
  213.         '   substring as a new one (providing there's content).
  214.  
  215.         If Not foundIt Then
  216.             If contents <> "" Then
  217.                 substringToAdd = key + "=" + contents
  218.                 GoSub AddSubstring
  219.             End If
  220.         End If
  221.  
  222.     Else                                         'no current contents in tag string
  223.         If contents <> "" Then                   'if user supplied contents,
  224.             substringToAdd = key + "=" + contents
  225.             GoSub AddSubstring
  226.         End If
  227.     End If
  228.  
  229.     '   Return the resulting tag string.
  230.  
  231.     theTagString = tagStringAccumulator
  232.     Exit Sub
  233.  
  234.  
  235. '   Add a substring to the end of the tag string accumulator.
  236.  
  237. AddSubstring:
  238.     If tagStringAccumulator <> "" Then
  239.         tagStringAccumulator = tagStringAccumulator + ";"
  240.     End If
  241.     tagStringAccumulator = tagStringAccumulator + substringToAdd
  242.     Return
  243.  
  244. End Sub
  245.  
  246.